﻿using SpyApp.Models;
using SpyApp.Models.BaseCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SpyApp.Controllers
{
    public class DeviceController : Controller
    {
        string _messageError = "";
        private UserRepository UseruserRepository;

        public DeviceController()
        {
            this.userRepository = new UserRepository();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult NoDevice()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult ListDevicePartial()
        {
            string username = System.Web.HttpContext.Current.User.Identity.Name;
            // Nghĩa: lấy list device
            IEnumerable<Device> list = this.userRepository.GetListDeviceByUserName(username);
            #region test
            //IEnumerable<Device> list = new List<Device>()
            //{
            //    FakeMake.FakeDevice(),
            //    FakeMake.FakeDevice(),
            //    FakeMake.FakeDevice(),
            //    FakeMake.FakeDevice(),
            //};
            #endregion
            return PartialView(list);
        }

        [ChildActionOnly]
        public ActionResult DeviceInfoPartial()
        {
            Device device = new Device();
            string deviceId = Request.QueryString["deviceid"];
            string username = System.Web.HttpContext.Current.User.Identity.Name;
            IEnumerable<Device> listDevice = this.userRepository.GetListDeviceByUserName(username);

            if (listDevice == null)
            {
                return null;
            }
            if (string.IsNullOrEmpty(deviceId))
            {
                device = listDevice.First();
            }
            else
            {
                device = userRepository.GetDeviceByUserName(deviceId, username);
            }
            // return
            
            SpyApp.Models.DeviceModel.DeviceInfo deviceInfo = new DeviceModel.DeviceInfo()
            {
                DeviceName = device.DeviceName,
                AppVersion = device.AppVersion,
                Imei1 = device.Imei1,
                Imei2= device.Imei2,
                OsBrand = device.OsBrand,
                OsVersion = device.OsVersion,
                Sn = device.Sn
            };
            return PartialView(deviceInfo);
        }

        public UserRepository userRepository { get; set; }
    }
}
